/**
 * 貌似是QQ空间验证码,有些用jquery实现
 *
 +-------------------------------------------------
 * @link
 * @copyright
 * @author Haijun Wu <nicholasinlove@126.com>
 +------------------------------------------------- 
 * 
 * $Id: verify.js 65 2011-03-25 07:09:12Z nicholasinlove1986@gmail.com $
 */

function hiddenVerify(isImme){
	if(!verifyCounter[0])
		return;
	if(!isImme)
		verifyCounter[0].timeout = setTimeout(function(){verifyCounter[0].killMe()},500);
	else
		verifyCounter[0].killMe();
}
	
VerifyImage.prototype.change = function(){
	/*var o = document.getElementById(this.eleID);
	this.seed = Math.random();
	o.src = URT+"/verify/"+this.seed;*/
	this.seed = Math.random();
	$('#'+this.eleID).attr('src',URT+'/verify/'+this.seed);
}

var verifyCounter=[];

function VerifyImage(eleID,x,y){
	var c = verifyCounter.length;
	this.timeout = null;
	this.ipt = null;
	if(!eleID)
		this.eleID = eleID = "verifyIMG_"+c;
	this.od = document.createElement("div");
	with(this.od){
		style.position = "absolute";
		style.backgroundColor = "#FFF";
		style.border = "solid 1px #ccc";
		style.padding = "2px";
		id = eleID+"_anchor";
		style.zIndex = 3000;
	}
	this.templete = ('<img id="'+eleID+'" onclick="getVerify(\''+eleID+'\','+c+')" style="cursor:pointer;margin-bottom:1px;" src="'+URT+'/verify/{{_random_}}" alt="Loading..." title="Refresh" width="60" height="25" /><br /><p onclick="getVerify(\''+eleID+'\','+c+')" style="color:#333;cursor:pointer;font-size:10px;text-align:center;">Refresh</p>');
	this.seed = Math.random();
	this.appended = false;
	verifyCounter[c] = this;
}

VerifyImage.prototype.killMe=function(){
	//this.od.style.display="none";
	$(this.od).fadeOut(400);
}

function getVerify(s,n){
	clearTimeout(verifyCounter[n].timeout);
	verifyCounter[n].change();
	//var i=$("verifycode");
	if(verifyCounter[n].ipt){
		verifyCounter[n].ipt.value = "";
		verifyCounter[n].ipt.focus();
	}
}

function focusGetVerify(o){
    if(o==undefined){
		return ;
    }
	var pos = getPosition(o);
	var e = (verifyCounter.length>0)?(verifyCounter[0]):(new VerifyImage());
	e.ipt = o;
	e.showMe(pos.left,pos.top);
	o.value="";
}

function getPosition(obj){   
	var top    = 110;   
	var left   = 0;   
	var width  = obj.offsetWidth;   
	var height = obj.offsetHeight;   
    while(obj.offsetParent){   
		top  += obj.offsetTop;   
		left += obj.offsetLeft;   
		obj  =  obj.offsetParent;   
    }   
	return {"top":top,"left":left,"width":width,"height":height};   
}
    
VerifyImage.prototype.showMe=function(x,y){
	with(this.od){
		style.top  = (y-85)+"px";
		style.left = (x+1)+"px";
	}
    if(document.readyState){
		if(!this.appended&&document.readyState=="complete"){
			document.body.appendChild(this.od);
			this.od.innerHTML=this.templete.replace(/\{\{_random_\}\}/g,this.seed); 
			this.appended=true;
		}
	}else{
		document.body.appendChild(this.od);
		this.od.innerHTML=this.templete.replace(/\{\{_random_\}\}/g,this.seed); 
		this.appended=true;
	}	
	//this.od.style.display="";
	$(this.od).fadeIn(200);
}